package com.jingbian.cppccbackend.controller.admin;

import com.jingbian.cppccbackend.entity.HomePageBanner;
import com.jingbian.cppccbackend.entity.Result;
import com.jingbian.cppccbackend.service.admin.HomePageBannerService;
import com.jingbian.cppccbackend.utils.AliOssUtil;
import com.jingbian.cppccbackend.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/admins")
public class adminFileUploadController {

    @Autowired
    private HomePageBannerService homePageBannerService;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file, Integer bannerId) throws Exception {
        if (file == null || file.isEmpty()) {
            return ResultUtil.fail("文件不能为空");
        }

        try {
            // 验证文件类型
            String contentType = file.getContentType();
            if (contentType == null || !contentType.startsWith("image/")) {
                return ResultUtil.fail("只能上传图片文件");
            }

            // 验证文件大小（2MB）
            if (file.getSize() > 2 * 1024 * 1024) {
                return ResultUtil.fail("文件大小不能超过2MB");
            }

            String originalFilename = file.getOriginalFilename();
            String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

            // 添加日志
            System.out.println("开始上传文件到OSS，文件名：" + filename);

            String url = AliOssUtil.uploadFile(filename, file.getInputStream());

            // 验证URL
            if (url == null || url.isEmpty()) {
                return ResultUtil.fail("文件上传到OSS失败");
            }

            // 更新数据库中的图片URL
            HomePageBanner banner = homePageBannerService.getById(bannerId);
            if (banner != null) {
                banner.setImageUrl(url);
                homePageBannerService.updateById(banner);
                return ResultUtil.success(url);
            } else {
                return ResultUtil.fail("未找到对应的轮播图记录");
            }
        } catch (Exception e) {
            System.err.println("文件上传失败：" + e.getMessage());
            e.printStackTrace();
            return ResultUtil.fail("文件上传失败：" + e.getMessage());
        }
    }

    @PostMapping("/uploadCoverImg")
    public Result<String> uploadCoverImg(MultipartFile file) throws Exception {
        //把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        System.out.println("文件名"+originalFilename);
        //保证文件的名字是唯一的,从而防止文件覆盖
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = AliOssUtil.uploadFile(filename,file.getInputStream());
        return ResultUtil.success(url);
    }
}
